package top.zy.model.event.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import top.zy.model.event.event.UserRegisterEvent;
import top.zy.model.event.service.UserService;

/**
 * @author ZackYoung
 * @version 1.0
 * @date 2021/12/26
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    ApplicationEventPublisher applicationEventPublisher;

    /**
     * Set the ApplicationEventPublisher that this object runs in.
     * <p>Invoked after population of normal bean properties but before an init
     * callback like InitializingBean's afterPropertiesSet or a custom init-method.
     * Invoked before ApplicationContextAware's setApplicationContext.
     *
     * @param applicationEventPublisher event publisher to be used by this object
     */
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    @Override
    public void register(String username) {
        //  执行注册逻辑
        log.info("[register][执行用户（{}）的注册逻辑]",username);

        // 发布
        applicationEventPublisher.publishEvent(new UserRegisterEvent(this, username));
    }
}
